<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.ResultSetMetaData"%>

<%! 
  String strTo(String st) throws Exception{ 
     byte[] bytes=st.getBytes("ISO-8859-1"); 
     String string=new String(bytes);
     return string;
  } 
  ArrayList<String[]> getDBItemNums(Connection conn)throws Exception{
	 ArrayList<String[]> list=new ArrayList<String[]>();
	 Statement st=conn.createStatement();
	 ResultSet rs=st.executeQuery("select * from comment");
	 while(rs.next()){ 
       list.add(new String[]{rs.getObject(2).toString(),rs.getObject(3).toString()});
	 }
	 return list;
  }
  void DBOperate(Connection conn,String clause)throws Exception{ 
	 Statement st=conn.createStatement();
	 int rs=st.executeUpdate(clause);
 }
 %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    /** 链接数据库参数 **/
    String driverName = "com.mysql.jdbc.Driver"; //驱动名称
    String DBUser = "root"; //mysql用户名
    String DBPasswd = ""; //mysql密码
    String DBName = "sanhaolu"; //数据库名

    //数据库完整链接地址
    String connUrl = "jdbc:mysql://localhost/" + DBName + "?user="
            + DBUser + "&password=" + DBPasswd;

    //加载数据库驱动
    Class.forName(driverName).newInstance();

    //链接数据库并保存到 conn 变量中
    Connection conn = DriverManager.getConnection(connUrl);
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<link type="text/css" rel="stylesheet" href="css/in.css">
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'sasion.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <script language="javascript">
    function loadMsg(){ 
    var table=document.getElementById("comment");
    table
    }
	function addElement() {
	//创建TextNode节点
	var person_name=form1.person.value;
	var comment_content=form1.content.value;
	var person = document.createTextNode(person_name);
	var content = document.createTextNode(comment_content);	
	//创建td类型的Element节点
	var td_person = document.createElement("td"); 
	var td_content = document.createElement("td");	
	var tr = document.createElement("tr"); //创建一个tr类型的Element节点
	var tbody = document.createElement("tbody"); //创建一个tbody类型的Element节点
	//将TextNode节点加入到td类型的节点中
	td_person.appendChild(person);
	td_content.appendChild(content);	
	//将td类型的节点添加到tr节点中
	tr.appendChild(td_person);
	tr.appendChild(td_content);	
	tbody.appendChild(tr); //将tr节点加入tbody中
	var tComment = document.getElementById("comment"); //获取table对象
	tComment.appendChild(tbody); //将节点tbody加入节点尾部
	form1.person.value="";	//清空评论人文本框
	form1.content.value="";		//清空评论内容文本框
	var flag="add";
	location.href="Transfer.jsp?name="+person_name+"&content="+comment_content+"&flag="+flag;  
}
//删除第一条评论
function deleteFirstE(){
	var tComment = document.getElementById("comment"); //获取table对象
	var person_name="";
	if(tComment.rows.length>1){
	    person_name=tComment.rows[1].cells[0].innerText;
		tComment.deleteRow(1);		//删除表格的第二行，即第一条评论，
	}
	
	var flag="minus";
	location.href="Transfer.jsp?name="+person_name+"&content=nothing"+"&flag="+flag;
}
//删除最后一条评论
function deleteLastE(){
	var tComment = document.getElementById("comment"); //获取table对象
	var person_name="";
	if(tComment.rows.length>1){
		person_name=tComment.rows[tComment.rows.length-1].cells[0].innerText;
		tComment.deleteRow(tComment.rows.length-1);	//删除表格的最后一行，即最后一条评论
	}
		var flag="minus";
	location.href="Transfer.jsp?name="+person_name+"&content=nothing"+"&flag="+flag;
}
	</script>
  
  <body>

   <table width="600" height="70" border="0" align="center" cellpadding="0" cellspacing="1" bordercolorlight="#FF9933" bordercolordark="#FFFFFF" bgcolor="#666666">
     <thead>
       <tr>
         <td width="14%" align="center" bgcolor="#FFFFFF"><img src="head.jpg" width="70" height="74"></td>
         <td width="86%" align="left" bgcolor="#FFFFFF">&nbsp;人生若真如一场大梦，这个梦倒也很有趣的。在这个大梦里，一定还有长长短短，深深浅浅，肥肥瘦瘦、甜甜苦苦，无数的小梦。有些已经随着日影飞去；有些还远着哩……</td>
       </tr>
     </thead>
   </table>
  <br>
   <table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bordercolorlight="#666666" bordercolordark="#FFFFFF" id="comment">
     <tr>
	   <td width="18%" height="27" align="center" bgcolor="#E5BB93">评论人</td>
	   <td width="82%" align="center" bgcolor="#E5BB93">评论内容</td>
     </tr>
     <% 
        ArrayList<String[]> list=getDBItemNums(conn);
        for(int i=0;i<list.size();i++){ 
      %>
      <tr >
	   <td width="18%" ><%=list.get(i)[0] %></td>
	   <td width="82%" ><%=list.get(i)[1]  %></td>
      </tr>
     <%} %>
   </table>
   <form name="form1" method="post" action="">    
     <table width="600" height="122" border="0" align="center" cellpadding="0" cellspacing="0">
       <tr>
         <td width="119" height="14">&nbsp;</td>
         <td width="481">&nbsp;</td>
       </tr>
       <tr>
         <td height="27" align="center">评 论 人：</td>
         <td>
           <input name="person" type="text" id="person" size="40">
         </td>
       </tr>
       <tr>
         <td align="center">评论内容：</td>
         <td><textarea name="content" cols="60" rows="6" id="content"></textarea></td>
       </tr>
       <tr>
         <td height="40">&nbsp;</td>
         <td><input name="Button" type="button" class="btn_grey" value="发表" onClick="addElement()" >
          &nbsp;
         <input name="Reset" type="reset" class="btn_grey" value="重置">
          &nbsp;
         <input name="Button" type="button" class="btn_grey" value="删除第一条评论" onClick="deleteFirstE()">
         &nbsp;
         <input name="Button" type="button" class="btn_grey" value="删除最后一条评论" onClick="deleteLastE()"></td>
       </tr>
    </table>
  </form>
 </body>
</html>
